/**
 * https://leetcode.cn/problems/interval-list-intersections/
 */
#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
        int i = 0, j = 0;
        vector<vector<int>> res;
        while (i < firstList.size() && j < secondList.size()) {
            int lhs = max(firstList[i][0], secondList[j][0]);
            int rhs = min(firstList[i][1], secondList[j][1]);
            if (lhs <= rhs) {
                res.emplace_back(vector{lhs, rhs});
            }
            if (firstList[i][1] < secondList[j][1]) {
                i++;
            } else {
                j++;
            }
        }
        return res;
    }
};


void display(vector<vector<int>> data) {
    std::cout << "[";
    for (const auto &item: data) {
        std::cout << "[" << item[0] << ", " << item[1] << "], ";
    }
    std::cout << "]" << std::endl;
}

int main() {
    Solution s;
    // @formatter:off
    vector<vector<int>> firstList = {{0,2},{5,10},{13,23},{24,25}};
    vector<vector<int>> secondList = {{1,5},{8,12},{15,24},{25,26}};
    // @formatter:on

    auto res = s.intervalIntersection(firstList, secondList);
    display(res);

    firstList = {{1, 3}, {5, 9}};
    secondList = {};
    res = s.intervalIntersection(firstList, secondList);
    display(res);
}